# get the time period

require "00_common"


module TimePeroid2
  def self.get_all_files
    Common.get_all_files("./Jan_6")
  end

  def self.handle_each_file(file_name, global_start_time, global_end_time)
    puts "now handling: " + file_name 

    raw_file = File.open "./Jan_6/" + file_name, "r"

    counter = 0
    begin
      while line = raw_file.readline
        if line.length == 0 or line[0,1] == "#"
          next
        end

        counter = counter + 1
        fields = line.split "|"

        local_time_stamp    = fields[LOCAL_TIME_STAMP]
        session_id          = fields[SESSION_ID]
        session_start_time  = fields[SESSION_START_TIME]
        client_ip           = fields[CLIENT_IP]
        server_ip           = fields[SERVER_IP]
        l4_proto            = fields[L4_PROTO]
        client_port         = fields[CLIENT_PORT]
        server_port         = fields[SERVER_PORT]
        # client_payload_hex  = 9
        # server_payload_hex  = 10
        protocol            = fields[PROTOCOL]
        client_payload      = fields[CLIENT_PAYLOAD]
        server_payload      = fields[SERVER_PAYLOAD]

        if (local_time_stamp <=> global_start_time) < 0
          global_start_time = local_time_stamp
        end

        if (local_time_stamp <=> global_start_time) > 0
          global_end_time = local_time_stamp
        end

        if counter % 100000 == 0
          puts counter
        end

      end

    rescue Exception => e
      puts e.to_s
      raw_file.close
    end

    puts "<start:#{global_start_time} == end:#{global_end_time}"
    period = Hash.new
    period["start"] = global_start_time
    period["end"] = global_end_time
    period
  end

  def self.handle_all_file(all_files)
    global_start_time = "12-29-2010 20:17"
    global_end_time = "12-29-2010 20:17"

    output_file = File.open "new_time_period.txt", "w"

    all_files.each do |file_name|
      period = handle_each_file file_name, global_start_time, global_end_time

      if (period["start"] <=> global_start_time) < 0
        global_start_time = period["start"]
      end

      if (period["end"] <=> global_start_time) > 0
        global_end_time = period["end"]
      end

      output_file.puts "<start:#{global_start_time} == end:#{global_end_time}"
    end

    output_file.close
  end
end

begin
  all_files = TimePeroid2.get_all_files
  TimePeroid2.handle_all_file all_files
rescue Exception => e
  puts e.to_s
end